package com.isnotlocus.gulimail.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;

import org.springframework.web.cors.reactive.CorsWebFilter; //一定要记住，要导reactive包下的这个类
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
//一定要记住，要导reactive包下的这个类，因为gateway 都是动态的
@Configuration
public class GulimailCorsConfiguration {

    /**
     * 配置跨域
     * @return
     */
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration=new CorsConfiguration();
        //1.配置跨域
        corsConfiguration.addAllowedHeader("*");  //允许头部的哪些信息
        corsConfiguration.addAllowedMethod("*");  //允许你哪些请求方式
        corsConfiguration.addAllowedOrigin("*");  //允许哪些来源
        corsConfiguration.setAllowCredentials(true); //是否允许携带cookie,不配置这个的话，配置cookie信息会丢失
        source.registerCorsConfiguration("/**",corsConfiguration);
        return  new CorsWebFilter(source);

    }
}
